import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  {
    path: '/',
    redirect: '/invitation'
  },
  {
    path: '/invitation',
    name: 'InvitationPage',
    component: () => import('@/views/invitation/InvitationPage.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/login',
    name: 'LoginPage',  
    component: () => import('@/views/auth/LoginPage.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/admin',
    name: 'Admin',
    component: () => import('@/views/admin/AdminDashBoard.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        redirect: '/admin/games',
    },
      {
        path: 'games',
        name: 'GamesListPage',
        component: () => import('@/views/admin/games/GamesListPage.vue')
      },
      {
        path: 'teachers',
        name: 'TeachersListPage',
        component: () => import('@/views/admin/teachers/TeachersListPage.vue')
      }
    ]
  },

]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token') // 可替换为你的登录状态判断逻辑

  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login')
  } else {
    next()
  }
})

export default router
